package com.dragon.pear.api.perfect;

import android.view.View;

import com.chris.dev.base.libs.v1.http.params.IdParam;
import com.chris.dev.base.libs.v1.http.params.IdsParam;
import com.chris.dev.base.libs.v1.http.params.NoneParam;
import com.chris.dev.base.libs.v1.http.params.PageParam;
import com.chris.dev.base.libs.v1.http.params.SearchSortPageParam;
import com.chris.dev.base.libs.v1.http.protocols.HttpResult;
import com.chris.dev.base.libs.v1.http.results.PageData;
import com.chris.dev.base.libs.v1.http.results.ValueResult;
import com.chris.libs.http.x.v1.CompleteCallback;
import com.chris.libs.http.x.v1.NetApi;
import com.dragon.pear.base.http.SuccessNetCallback;
import com.dragon.pear.base.http.SuccessStatusNetCallback;
import com.dragon.pear.base.http.DefaultRetrofit;
import com.dragon.pear.base.http.TokenUtil;
import com.dragon.pear.model.params.create.UaaAccountCreateParam;
import com.dragon.pear.model.params.order.UaaAccountOrderParam;
import com.dragon.pear.model.params.search.UaaAccountSearchParam;
import com.dragon.pear.model.params.update.UaaAccountUpdateParam;
import com.dragon.pear.model.results.UaaAccountResult;

import java.util.List;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;

/**
 * @author Chris Chan
 * Create on 2025-02-17 20:36
 * Use for: 账号 接口调用
 * Explain: 标准模式
 * 不用在调用的时候构建Token和body
 * 底部附有使用方法
 */
public class UaaAccountApi extends NetApi {
    public static Service service = DefaultRetrofit.create("uaa/account", Service.class);

    /**
     * 增
     *
     * @param params
     * @param callback
     */
    public static void add(UaaAccountCreateParam params, SuccessNetCallback<ValueResult<Boolean>> callback) {
        String token = TokenUtil.getAuthorization();
        RequestBody requestBody = postRequestObjBody(null, params);
        service.add(token, requestBody).enqueue(callback);
    }

    /**
     * 删
     *
     * @param params
     * @param callback
     */
    public static void remove(IdParam<Long> params, SuccessNetCallback<ValueResult<Boolean>> callback) {
        String token = TokenUtil.getAuthorization();
        RequestBody requestBody = postRequestObjBody(null, params);
        service.remove(token, requestBody).enqueue(callback);
    }

    /**
     * 删
     *
     * @param params
     * @param callback
     */
    public static void removeAllByIds(IdsParam<Long> params, SuccessNetCallback<ValueResult<Boolean>> callback) {
        String token = TokenUtil.getAuthorization();
        RequestBody requestBody = postRequestObjBody(null, params);
        service.removeAllByIds(token, requestBody).enqueue(callback);
    }

    /**
     * 改
     *
     * @param params
     * @param callback
     */
    public static void update(UaaAccountUpdateParam params, SuccessNetCallback<ValueResult<Boolean>> callback) {
        String token = TokenUtil.getAuthorization();
        RequestBody requestBody = postRequestObjBody(null, params);
        service.update(token, requestBody).enqueue(callback);
    }

    /**
     * 查 全部
     *
     * @param params
     * @param ownView
     * @param callback
     */
    public static void findAll(NoneParam params, View ownView, CompleteCallback<List<UaaAccountResult>> callback) {
        String token = TokenUtil.getAuthorization();
        RequestBody requestBody = postRequestObjBody(null, params);
        service.findAll(token, requestBody).enqueue(new SuccessStatusNetCallback<>(callback).with(ownView));
    }

    /**
     * 查 分页
     *
     * @param params
     * @param ownView
     * @param callback
     */
    public static void findPage(PageParam params, View ownView, CompleteCallback<PageData<UaaAccountResult>> callback) {
        String token = TokenUtil.getAuthorization();
        RequestBody requestBody = postRequestObjBody(null, params);
        service.findPage(token, requestBody).enqueue(new SuccessStatusNetCallback<>(callback).with(ownView));
    }

    /**
     * 搜索
     *
     * @param params
     * @param ownView
     * @param callback
     */
    public static void search(SearchSortPageParam<UaaAccountSearchParam, UaaAccountOrderParam> params, View ownView, CompleteCallback<PageData<UaaAccountResult>> callback) {
        String token = TokenUtil.getAuthorization();
        RequestBody requestBody = postRequestObjBody(null, params);
        service.search(token, requestBody).enqueue(new SuccessStatusNetCallback<>(callback).with(ownView));
    }

    public interface Service {
        // 增
        @POST("add")
        Call<HttpResult<ValueResult<Boolean>>> add(@Header("Authorization") String token, @Body RequestBody body);

        // 删
        @POST("remove")
        Call<HttpResult<ValueResult<Boolean>>> remove(@Header("Authorization") String token, @Body RequestBody body);

        // 删
        @POST("removeByIds")
        Call<HttpResult<ValueResult<Boolean>>> removeAllByIds(@Header("Authorization") String token, @Body RequestBody body);

        // 改
        @POST("update")
        Call<HttpResult<ValueResult<Boolean>>> update(@Header("Authorization") String token, @Body RequestBody body);

        // 查
        @POST("findAll")
        Call<HttpResult<List<UaaAccountResult>>> findAll(@Header("Authorization") String token, @Body RequestBody body);

        // 查分页
        @POST("findPage")
        Call<HttpResult<PageData<UaaAccountResult>>> findPage(@Header("Authorization") String token, @Body RequestBody body);

        // 搜索
        @POST("search")
        Call<HttpResult<PageData<UaaAccountResult>>> search(@Header("Authorization") String token, @Body RequestBody body);
    }
}

/*
    // 使用方法 直接在Presenter中使用
    UaaAccountApi.remove(IdParam.create(12138L), new SuccessNetCallback<ValueResult<Boolean>>() {
        @Override
        protected void onSuccess(ValueResult<Boolean> data) {
            System.out.println(data.getValue());
        }
    });
*/
